Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

315
Visualizações
is `file_get_contents("php://input")` the only way to get post data from `fetch` and is it safe to use async- and hack-wise?

I'm trying to rewrite my JQuery $.post() code via native fetch() function. And it seems like the only way to do it with PHP server code is using file_get_contents("php://input"). I do it like it is shown here and here:

js code:

fetch('/myscript.php', {
    method: 'post',
    mode: "same-origin",
    credentials: "same-origin",
    body: JSON.stringify({par1:par1, par2:par2})
}).then(response => response.text())
.then(output => {
  // do stuff
});

myscript.php:

$input = json_decode(file_get_contents('php://input'), true);
// do stuff with $input['par1'], $input['par2']
echo $output;

There are two things which makes me worried:

  1. The project has more than one fetch call like this. What if two fetch functions are called simultaneously with two different php script files? They both will access php://input at the same time. Won't it lead to a conflict?

  2. With $.post() I was able to check if myscript.php hasn't been called directly by a hacker with this line of code:

    if ($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') die('Hack attempt!');

    but with the new approach $_SERVER['HTTP_X_REQUESTED_WITH'] is undefined for some reason. Is there a way to ensure myscript.php is called with fetch or php://input ensures it automatically?

  3. fetch is pretty modern function, but json_decode(file_get_contents('php://input'), true) looks pretty weird and intended for some other use case. Isn't there a better way to fetch data from a php-based server?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

  1. No. Each script invocation has its own php://input.
  2. How does that check if it's called by a hacker? Nothing stops a hacker from sending that header. But if you really want it, you can add the header yourself (which is what a hacker would do):
fetch('/myscript.php', {
    method: 'post',
    mode: "same-origin",
    headers: {"X-Requested-With": "XMLHttpRequest"},
    credentials: "same-origin",
    body: JSON.stringify({par1:par1, par2:par2})
}).then(response => response.text())
.then(output => {
  // do stuff
});

  1. You can send url-encoded parameters instead of JSON. Then PHP will parse them into $_POST as with normal forms.
fetch('/myscript.php', {
    method: 'post',
    mode: "same-origin",
    headers: {
        "X-Requested-With": "XMLHttpRequest", 
        "Content-type": "application/x-www-form-urlencoded"
    },
    credentials: "same-origin",
    body: `par1=${encodeURIComponent(par1)}&par2=${encodeURIComponent(par2)}`
}).then(response => response.text())
.then(output => {
  // do stuff
});
about 4 years ago · Juan Pablo Isaza Relatório

0

  1. Each new process will get a separate "input", there is no risk of conflict.
  2. You want CSRF tokens. Anyone can send a XMLHttpRequest
  3. you are comparing oranges to apples. php://input is fine.

php://input documentation

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda